<?php


namespace app\zhongmaotong\controller;


use app\zhongmaotong\model\SpModel;

// use app\zhengmaotong\validate\GoodsValidate;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

class SpController extends AdminBaseController
{
    //显示首页
    public function index()
    {
        //获取查询数据
        $name = input('title');
        $where = [];
        //判断是否有值
        if ($name) {
            $where[] = ['title', 'like', '%' . $name . '%'];
        }
        //查询排序分页
        $list = SpModel::order('id DESC')
            ->where($where)
            ->paginate(10);
        return $this->fetch('index', [
            'list' => $list,
        ]);
    }

    //添加
    public function add()
    {
        $list = SpModel::select();
        return $this->fetch('add', [
            'list' => $list,
        ]);
    }

    public function addPost()
    {
        $data = input('post.');
        $data['images'] = implode(',', $data['images']);
        /*$validate = new GoodsValidate();
        $result = $validate->scene('add')->check($data);
        if(true !== $result){
            $this->error($validate->getError());
        }*/
        //入库
        $data['create'] = time();
        if (SpModel::create($data)) {
            $this->success('添加成功', 'Sp/index');
        } else {
            $this->error('添加失败');
        }
    }

    //修改
    public function edit()
    {
        $id = input('id');
        $list = SpModel::find($id);
        return $this->fetch('edit', [
            'list' => $list,
        ]);
    }

    public function editPost()
    {
        $data = input('post.');
        //验证器
        /*$validate = new GoodsValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }*/
        //查询是否有此记录
        $list = SpModel::find($data['id']);
        if (!$list) {
            $this->error('未查询到该记录');
        }
        if ($list->update($data)) {
            $this->success('修改成功', 'Sp/index');
        } else {
            $this->error('修改失败');
        }
    }

    //删除
    public function delete()
    {
        $id = input('id/d', '');
        $del = SpModel::destroy($id);
        if ($del) {
            $this->success('删除成功', 'Sp/index');
        } else {
            $this->error('删除失败');
        }
    }

    //显示隐藏内容
    public function yc(Request $request)
    {
        $id = $request->param('id');
        if (!$id) {
            $this->error('未查询到该id');
        }
        $goods = SpModel::find($id);
        $source = null;
        if ($goods['self_status'] == 1) {
            $source = 0;
        }
        if ($goods['self_status'] == 0) {
            $source = 1;
        }
        if (SpModel::update(['self_status' => $source], ['id' => $id])) {
            $this->success('修改成功', 'Sp/index');
        } else {
            $this->error('修改失败');
        }
    }

    // 打开配置页面
    public function pz()
    {
        $goodsId = input('goods_id');
        $list = \think\facade\Db::name('goods_details')
            ->where('goods_id', $goodsId)
            ->select();
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr', ['list' => $list]);
    }

    // 配置提交
    public function pzPost()
    {
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
//        if (count($attrs) - 1 < 1) {
//            $this->error('请至少配置一个属性！');
//        }

        \think\facade\Db::name('goods_details')
            ->where('goods_id', $goodsId)
            ->delete();
        $insert_data = [];
        foreach ($attrs as $attr) {
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr
            ];
        }
        \think\facade\Db::name('goods_details')->insertAll($insert_data);
        $this->success('保存成功');
    }

    public function pzValue()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr_value');
    }

    public function GetGoodsAttrValue()
    {
        $goodsId = input('goods_id');
        $attrs = \think\facade\Db::name('goods_details')
            ->where('goods_id', $goodsId)
            ->select();
        return json(['code' => 1, 'msg' => '商品属性', 'data' => $attrs]);
    }

    public function getGoodsAttr2()
    {
        $goodsId = input('goods_id');
        $attrs = \think\facade\Db::name('goods_details')
            ->where('goods_id', $goodsId)
            ->select();
        return json(['code'=>1,'msg'=>'商品属性','data'=>$attrs]);
    }
    public function pzValuePost()
    {
        $data = input('post.form');
//        if(count($attrs)-1 < 1){
//            $this->error('请至少配置一个属性！');
//        }
        $insert_data = [];
        foreach ($data as $attr) {
            $insert_data[] = [
                'value' => $attr['attr_value'],
                'details_id' => $attr['attr']
            ];
        }
        $insert = \think\facade\Db::name('goods_value')->insertAll($insert_data);
        if ($insert) {
            return json(['code' => 1, 'msg' => '添加成功', 'data' => "NULL"]);
        } else {
            return json(['code' => 0, 'msg' => '添加失败', 'data' => "NULL"]);
        }

    }
}